﻿using Prism.Commands;
using Prism.Dialogs;
using Prism.Mvvm;
using Rafy.Domain;
using SwaggerSdk;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;

namespace Rafy.Book.ViewModels
{
    public class ConfigPageViewModel : BindableBase
    {
        private readonly IDialogService _dialogService;

        private List<RspBook> _books;
        public List<RspBook> Books
        {
            get => _books;
            set => SetProperty(ref _books, value);
        }
        public int bookId { get; set; } = 0; // 书本ID
        private RspBook _selectedBook;
        public RspBook SelectedBook
        {
            get => _selectedBook;
            set
            {
                if (SetProperty(ref _selectedBook, value) && value != null)
                {
                    bookId= value.Id;
                    Console.WriteLine("选择的书本ID" + bookId);
                    LoadCatalogBooks(value.Id); // 加载目录数据
                }
            }
        }
        private ObservableCollection<Base_CatalogBook> _catalogBooks;
        public ObservableCollection<Base_CatalogBook> CatalogBooks
        {
            get => _catalogBooks;
            set => SetProperty(ref _catalogBooks, value);
        }


        public DelegateCommand<int?> AddSubject { get; private set; }
        public DelegateCommand<int?> AddCatalogCommand { get; private set; }
        public DelegateCommand<int?> DeleteCommand { get; private set; }
        public DelegateCommand<int?> DelCommand { get; private set; }
        public ConfigPageViewModel(IDialogService dialogService)
        {
            _dialogService = dialogService;
            AddSubject = new DelegateCommand<int?>(AddSubjectEvent);
            AddCatalogCommand = new DelegateCommand<int?>(AddCatalogCommandEvent);
            DeleteCommand = new DelegateCommand<int?>(DeleteCommandEvent);
            DelCommand = new DelegateCommand<int?>(DelCommandEvent);
            LoadBooks();
        }

        

        /// <summary>
        /// 添加章节
        /// </summary>
        private void AddCatalogCommandEvent(int? Id)
        {
            Console.WriteLine($"AddCatalog bookId={bookId} cataId={Id}");
            if (bookId < 1)
            {
                HandyControl.Controls.Growl.Warning("请选择课目", "InfoMessage");
                return;
            }
            //给表单传值 
            DialogParameters param = new DialogParameters();
            param.Add("bookId", bookId);//传递参数
            param.Add("cataId", Id);//传递参数
            _dialogService.ShowDialog("FmKeMu", param, arg =>
            {
                if (arg.Result == ButtonResult.OK)
                {
                    LoadCatalogBooks(bookId);
                    //var result = arg.Parameters.GetValues<string>("name");//返回参数
                }
            });
        }
        private void DelCommandEvent(int? Id)
        {
            var result = MessageBox.Show("确认删除该项吗？", "删除确认", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                var repo = RF.ResolveInstance<Base_CatalogBookRepository>();
                var obj = repo.GetById(Id);
                obj.PersistenceStatus = PersistenceStatus.Deleted;
                repo.Save(obj);
                LoadCatalogBooks(bookId);
            }
            
        }
        /// <summary>
        /// 添加书本
        /// </summary>
        private void AddSubjectEvent(int? Id)
        {
            //给表单传值 
            DialogParameters param = new DialogParameters();
            param.Add("bookId", Id);//传递参数
            _dialogService.ShowDialog("FmSubject", param, arg =>
            {
                if (arg.Result == ButtonResult.OK)
                {
                    LoadBooks();
                    //var result = arg.Parameters.GetValues<string>("name");//返回参数
                }
            });
        }
        /// <summary>
        /// 删除书本
        /// </summary>
        private void DeleteCommandEvent(int? Id)
        {
            var result = MessageBox.Show("确认删除该项吗？", "删除确认", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (result == MessageBoxResult.Yes)
            {
                var rp = RF.ResolveInstance<Base_ButtonRepository>();
                var lst = rp.GetsBy(Id);
                lst.Clear();
                rp.Save(lst);
                var po = RF.ResolveInstance<Base_CatalogBookRepository>();
                var ts = po.GetsBy(Id);
                ts.Clear();
                po.Save(ts);

                var repo = RF.ResolveInstance<Base_BooksRepository>();
                var book = repo.GetById(Id);
                book.PersistenceStatus = PersistenceStatus.Deleted;
                repo.Save(book);
            }
               
            LoadBooks();
        }
        private void LoadCatalogBooks(int bookId)
        {
            var repo = RF.ResolveInstance<Base_CatalogBookRepository>();
            CatalogBooks = repo.GetsBy(bookId);
        }




        private void LoadBooks()
        {
            var repo = RF.ResolveInstance<Base_BooksRepository>();
            var lst = repo.GetAll();
            var rp = RF.ResolveInstance<Base_ButtonRepository>();
            var list = new List<RspBook>();
            foreach (var item in lst)
            {
                var book = new RspBook();
                book.Id = item.Id;
                book.Name = item.F_BookName;
                var btns = rp.GetsBy(item.Id);
                if (btns != null && btns.Count>0)
                    book.Buttons = string.Join(',', btns.Select(s => s.F_BtnName));
                list.Add(book);
            }
            Books = list;
        }
    }
}
